/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.smarttv.querygraph;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.rdf.model.Resource;
import edu.yonsei.iwb.smarttv.model.ConceptArc;
import edu.yonsei.iwb.smarttv.model.MArc;
import edu.yonsei.iwb.smarttv.model.MNode;
import edu.yonsei.iwb.smarttv.querygraph.algorithm.ShortestPath;
import edu.yonsei.iwb.smarttv.util.UtilMethodSet;
import java.util.Hashtable;
import java.util.Vector;

/**
 *
 * @author Onlyjiny
 */
public class VariableMap {
    
    private Hashtable<Integer, Node[]> varMapByIndex = new Hashtable<Integer, Node[]>(); // MArc의 index와 헤드, 테일의 변수노드 저장
    private Vector<Resource> className = new Vector<Resource>();
    
//    private Vector<MArc> arcs = new Vector<MArc>();
    private Vector<Node> variable = new Vector<Node>();
    
    private int v_num;

    public VariableMap(ShortestPath sp, ConceptArc ca, int v_num) {
        this.v_num = v_num;
        
        for (int i = 0; i < sp.size()-1; i++) {            
            Node[] ns = new Node[2];
            Node tailNode = Node.createVariable(UtilMethodSet.getVariableCharacter(this.v_num++));
            ns[1] = tailNode;
            this.varMapByIndex.put(i, ns);                        
        }
        
        Node lastTailNode = ca.getTail().getVariableNode();
        Node[] lastns = new Node[2];
        lastns[1] = lastTailNode;
        this.varMapByIndex.put(sp.size()-1, lastns);
                        
        Node firstHeadNode = ca.getHead().getVariableNode();        
        Node[] firstns = this.varMapByIndex.get(0);        
        firstns[0] = firstHeadNode;        
                
        for (int j = 1; j < sp.size(); j++) {
            Node[] prevns = this.varMapByIndex.get(j-1);
            Node[] nextns = this.varMapByIndex.get(j);            
            Node headNode = prevns[1];
            nextns[0] = headNode;                        
        }
        
    }
    
    public VariableMap(Vector<Resource> classname, Vector<Node> variable){
        this.className = classname;
        this.variable = variable;       
    }
    
    public Hashtable<Integer, Node[]> getVarMapByIndex(){
        return this.varMapByIndex;
    }

    public int getVariableSeq() {
        return this.v_num;
    }
    
    public Node getVariablesAsPathIndex(int index) {
        return this.variable.get(index);
    }

    public Vector<Node> getVariablesAsClass(Resource className) {
        Vector<Node> result = new Vector<Node>();
        for (int i = 0; i < this.className.size(); i++) {
            Resource r = this.className.get(i);
            if (r.equals(className)) {
                result.add(this.variable.get(i));
            }
        }
        return result;
    }

    public Vector<Node> getAllNodes() {
        Vector<Node> variables = new Vector<Node>();
        for (int i = 0; i < this.variable.size(); i++) {
            Node var = this.variable.get(i);
            variables.add(var);
        }
        return variables;
    }

    public Vector<Node> getAllVariables() {
        Vector<Node> variables = new Vector<Node>();
        for (int i = 0; i < this.variable.size(); i++) {
            Node var = this.variable.get(i);
            if (var.isVariable()) {
                variables.add(var);
            }
        }
        return variables;
    }

    public Vector<Resource> getAllClasses() {
        Vector<Resource> classname = new Vector<Resource>();
        for (int i = 0; i < this.className.size(); i++) {
            Resource r = this.className.get(i);
            if (r.isResource()) {
                classname.add(r);
            }
        }
        return classname;
    }

    public Resource getClassTypeOfVariable(Node var) {
        int pos = this.variable.indexOf(var);
        return this.className.get(pos);
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < this.className.size(); i++) {
            sb.append(this.className.get(i).getURI() + ": " + this.variable.get(i).toString() + "\n");
        }
        return sb.toString();
    }
}
